<?php
/** @var yii\web\View $this */
use yii\helpers\Html;
use yii\helpers\Url;

$this->title = '二维码';
$this->params['breadcrumbs'][] = $this->title;
?>
<?= Html::beginForm(['generate-ticket'], 'post', ['id' => 'generate_ticket_form']) ?>
<div class="form-group">
    <label class="control-label">过期时间</label>
    <?= Html::textInput('expireSeconds', '', ['class'=>'form-control']) ?>
</div>
<div class="form-group">
    <label class="control-label">二维码类型</label>
    <?= Html::dropDownList('actionName', '', [
        'QR_SCENE' => '临时',
        'QR_LIMIT_SCENE' => '永久',
        'QR_LIMIT_STR_SCENE' => '永久的字符串参数值',
    ], ['class' => 'form-control']) ?>
</div>
<div class="form-group">
    <label class="control-label">场景id</label>
    <?= Html::textInput('sceneId', '', ['class' => 'form-control']) ?>
</div>
<div class="form-group">
    <label class="control-label">场景字符串</label>
    <?= Html::textInput('sceneStr', '', ['class' => 'form-control']) ?>
</div>
<div class="form-group">
    <button class="btn btn-primary">生成ticket</button>
</div>
<?= Html::endForm() ?>
<pre id="result"></pre>
<?= Html::beginForm(['qrcodeurl'], 'post', ['id' => 'qrcode_form']) ?>
<div class="form-group">
    <input type="text" id="ticket" name="ticket">
    <button>显示二维码</button>
</div>
<?= Html::endForm() ?>
<div id="show_qrcode"></div>
<?php
$js = <<<'EOT'
$('#generate_ticket_form').submit(function(e){
    $.ajax({
        type: 'post',
        url: this.action,
        data: $(this).serialize()
    }).done(function(resp){
        if (resp.error == 0) {
         alert('生成成功');
         $('#result').html(resp.data);
         $('#ticket').val(resp.data.ticket);
        }
    }).fail(function() {
        alert('生成失败');
    });
    return false;
});
$('#qrcode_form').submit(function(e) {
    $.ajax({
        type: 'post',
        url: this.action,
        data: $(this).serialize()
    }).done(function(resp) {
        if (resp.error == 0) {
            $('#show_qrcode').html('<img src="' + resp.url + '" alt="二维码">');
        }
    }).fail(function() {
        alert('显示失败');
    });
    return false;
});
EOT;
$this->registerJs($js);